#include "integral.hpp"


using namespace std;

void ThreadFunc::operator()()
{
    while ( true)
    {
        bool T = true;
        //_monitorRcv_ptr->Receive( T);
        rcvTask();
        if ( _data.precision < 0.0 || !T )
        {
            if ( !T)
            {
                //cout << T << endl;
            }
            return;
        }
        processData();
        sndResult();
    }
}

void ThreadFunc::rcvTask()
{
    _data = _monitorRcv_ptr->Receive();
}
void ThreadFunc::processData()
{
    _result = 0.0;

    double current = _data.a;
    double step    = _data.precision;

    while ( current < _data.b)
    {
        double a = current;
        double b = current + step;

        double f_a = _FUNC( a);
        double f_b = _FUNC( b);

        _result += ( b - a) * ( f_a +  f_b)/2;

        current += step;
    }
    double a = current;
    double b = _data.b;

    double f_a = _FUNC( a);
    double f_b = _FUNC( b);

    _result += ( b - a) * ( f_a +  f_b)/2;
}

void ThreadFunc::sndResult()
{
    _monitorSnd_ptr->Send( _result);
}

void ThreadFunc::Init( Monitor< data_t> &task_monitor, Monitor< double> &result_monitor)
{
    _monitorRcv_ptr = &task_monitor;
    _monitorSnd_ptr = &result_monitor;
}

ThreadFunc::ThreadFunc( Monitor< data_t> &task_monitor, Monitor< double> &result_monitor)
{
    _monitorRcv_ptr = &task_monitor;
    _monitorSnd_ptr = &result_monitor;
}

ThreadFunc::ThreadFunc()
{
    _data.a = 0;
    _data.b = 100;
    _data.precision = 0.001;

}
ThreadFunc::~ThreadFunc()
{


}
